
String.prototype.parseUrl = function(){
	var result = this;
	var index = result.indexOf("?") + 1;
	var result = result.slice(index);
	var result = result.split("&");
	var obj = {}
	result.forEach(function(item){
		var arr = item.split("=");
		obj[arr[0]] = arr[1]
	})
	return obj;
}
	var express = require("express");
	var server = express();
	var mysql = require("mysql");
	var expressStatic = require("express-static");
	var count=0;
	var sql = mysql.createConnection({
		host:"localhost",
		user:"root",
		password:"admin",
		database:"dawei",
	})
	// 链接
sql.connect()
server.get("/getdata",(request,response)=>{
	sql.query("select * from mingdan",function(error,data){
					if(!error){
						response.end(JSON.stringify(data));
					}
				})

})
server.get("/tianjia",(request,response)=>{
	var name = request.url.parseUrl().name;
	var sex = request.url.parseUrl().sex;
	var date = request.url.parseUrl().date;
	var department = request.url.parseUrl().department;
	var salary = request.url.parseUrl().salary;
	var mob = request.url.parseUrl().mob;
	var state = request.url.parseUrl().state;
	sql.query(`insert into mingdan (name,sex,date,department,salary,mob,state) values ("${name}","${sex}","${date}","${department}","${salary}","${mob}","${state}")`,function(error,data){
			if(error){
				response.end("error");
				console.log("error");
			} 
			else{
				sql.query("select * from mingdan","utf-8",function(error,data){
					if(!error){
						response.end(JSON.stringify(data));
					}
				})

			}
		})
})
server.get("/del",(request,response)=>{
	var idd = request.url.parseUrl().id;
	sql.query(`delete from mingdan where id="${idd}"`,function(error,data){
			if(error){
				response.end("error");
				console.log("error");
			} 
			else{
				response.end("删除成功");
			}
		})
})

	server.use(expressStatic(__dirname+'/tanchu'))
	server.listen(81);